﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model.Group;
using System.Web;

namespace YouHP.Pages.BBS
{
    public class Topic:TopicBasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected List<ReplyInfo> ReplyDataList;
        protected List<ReplyInfo> AllReplyDataList; // Ugly!! Change this!!!
        protected int PageCount;
        protected int PageIndex;
        protected string PagingString = "";
        protected int DataCount = 0;
        protected int allRepliesCount = 0;
        protected string[] tagArrSplit = null; 

        protected void HandleCommon()
        {
            this.SetTitle(TopicData.Title);
            this.AddLinkCss("/support/Alpha/css/topic.css");
            this.AddScriptSrc("/support/Alpha/Js/mootools.js");
        }

        protected void HandlePost()
        {
           
        }

        protected void HandleGet()
        {
            //更新帖子的点击数
            if (TopicData.Id > 0)
            {
                TopicBLL.Instance.UpdateTopicHits(TopicData.Id);
            }

            PageIndex = RequestTools.GetQueryInt("page", 1);
            ReplyDataList = ReplyBLL.Instance.GetReplyList(TopicData.Id, Config.GroupReplyPageSize, PageIndex, out DataCount);
            AllReplyDataList = ReplyBLL.Instance.GetReplyList(TopicData.Id, int.MaxValue, 1, out allRepliesCount);
            if (TopicData.Tags != "")
            {
                tagArrSplit = Regex.Split(TopicData.Tags, "\\s+");
            }
            PageCount = Utils.GetPageCount(DataCount, Config.GroupReplyPageSize);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PagingString = this.GetPagingString(
                PageIndex,
                PageCount,
                "/BBS/Topic.aspx" + query, "page");

        }

        protected bool CanReply(TopicInfo info)
        {
            bool retVal = false ;
            if(UserData.Id > 0 && UserData.Id != info.PostUserId)
            {
                retVal = true;
            }

            return retVal;
        }

        protected bool CanEdit(TopicInfo info)
        {
            bool retVal = false;
            if (UserData.Id == info.PostUserId)
            {
                retVal = true;
            }

            for (int i = 0; i < moderatorsId.Length; i++)
            {
                if (UserData.Id == moderatorsId[i])
                {
                    retVal = true;
                    break;
                }
            }

            return retVal;
        }

        protected bool CanDelete(TopicInfo info)
        {
            bool retVal = false;
            if (UserData.Id == info.PostUserId)
            {
                retVal = true;
            }
            for (int i = 0; i < moderatorsId.Length; i++)
            {
                if (UserData.Id == moderatorsId[i])
                {
                    retVal = true;
                    break;
                }
            }

            return retVal;
        }

        protected bool CanReply(ReplyInfo info)
        {
            bool retVal = false;
            if (UserData.Id > 0 && UserData.Id != info.PostUserId)
            {
                retVal = true;
            }

            return retVal;
        }

        protected bool CanEdit(ReplyInfo info)
        {
            bool retVal = false;
            if (UserData.Id == info.PostUserId)
            {
                retVal = true;
            }

            return retVal;
        }

        protected bool CanDelete(ReplyInfo info)
        {
            bool retVal = false;
            if (UserData.Id == info.PostUserId)
            {
                retVal = true;
            }

            for (int i = 0; i < moderatorsId.Length; i++)
            {
                if (UserData.Id == moderatorsId[i])
                {
                    retVal = true;
                    break;
                }
            }

            return retVal;
        }
        protected bool CanBanPoster(TopicInfo info)
        {
            bool retVal = false;

            for (int i = 0; i < moderatorsId.Length; i++)
            {
                if (info.PostUserId == moderatorsId[i])
                {
                    return retVal;
                }
            }
            for (int i = 0; i < moderatorsId.Length; i++)
            {
                if (UserData.Id == moderatorsId[i])
                {
                    retVal = true;
                    break;
                }
            }

            return retVal;
        }
        protected bool CanBanPoster(ReplyInfo info)
        {
            bool retVal = false;

            for (int i = 0; i < moderatorsId.Length; i++)
            {
                if (info.PostUserId == moderatorsId[i])
                {
                    return retVal;
                }
            }
            for (int i = 0; i < moderatorsId.Length; i++)
            {
                if (UserData.Id == moderatorsId[i])
                {
                    retVal = true;
                    break;
                }
            }

            return retVal;
        }
    }
}